from django.db import models
from django.core import validators

#This import is for thumbnails. :) :)
from django.contrib.thumbnails import *



class Artwork(models.Model):
    tags = models.ManyToManyField('Tag', blank=True, null=True)
    image_file = models.ImageField(upload_to="artwork")
    title = models.CharField(max_length=256, core=True)
    slug = models.SlugField(max_length=50, prepopulate_from=("year", "title", ), help_text="This field will automatically populate. Ignore it.", unique=True)
    artist = models.ForeignKey('Artist', blank=True, null=True)
    commentary = models.TextField(max_length=4096, blank=True, help_text="HTML is allowed (probably JavaScript too)")
    inch_height = models.FloatField(null=True, blank=True)
    inch_width = models.FloatField(null=True, blank=True, help_text="Enter a decimal number here, like 42.25 or leave blank.")
    medium = models.CharField(max_length=256, blank=True, help_text="(This may be left blank)")
    year = models.CharField(max_length=4, blank=True, validator_list=[validators.isOnlyDigits], help_text="Enter a four-digit year, like 1982 or leave blank")
    is_visible_yearly = models.BooleanField(default=True, help_text="Uncheck this to exclude this artwork from yearly galleries.  This is useful if you want this artwork to only appear in its tag galleries.")

    is_for_sale = models.NullBooleanField()
    has_been_sold = models.NullBooleanField()
    has_been_sold.short_description = "Sold" #This should be for the admin interface list view.
    price = models.DecimalField(max_digits=10,decimal_places=2, null=True, blank=True)
    

    def _admin_thumb(self):
	"""Returns an <img> tag pointing to a generated thumbnail file, suitable for the admin interface list view."""
        image_file_handle = open(self.get_image_file_filename())
        this_thumb = Thumbnail("admin_thumbs/"+self.image_file, size=(96,96), data=image_file_handle.read())
        image_file_handle.close()
        return '<img height="%s" width="%s" alt="%s" src="%s" />' % (this_thumb.thumbnail.size[1], this_thumb.thumbnail.size[0], self.title, this_thumb.url)
    _admin_thumb.allow_tags = True
    _admin_thumb.short_description = "Thumbnail"

    class Meta:
        verbose_name_plural = "Artworks"
        verbose_name = "Artwork"
		
    class Admin:
        fields = (
            (None, {
                'fields': ('image_file', 'title', 'slug',)
            }),
            ('Additional options', {
                'fields': ('artist', ('inch_height', 'inch_width'), 'medium', 'year', 'is_visible_yearly', 'tags', 'commentary',)
            }),
            ('Pricing', {
                'classes': 'collapse',
                'fields': ('is_for_sale', 'has_been_sold', 'price')
            }),
        )
        list_display = ('_admin_thumb', 'slug', 'artist', 'image_file', 'is_for_sale', 'has_been_sold', 'price', 'is_visible_yearly')
        list_filter = ('artist', 'year', 'tags', 'is_for_sale', 'is_visible_yearly')
        search_fields = ('slug', 'title', 'image_file')
    
    def __unicode__(self):
        return u'%s' % self.title

class Tag(models.Model):
    name = models.SlugField(primary_key=True) #This REALLY should be unique=, not primary_key=  --ADV  11-05-07
    is_visible = models.BooleanField(default=True, help_text="If TRUE, this tag's gallery will appear on your front page.")
	
    class Meta:
        verbose_name = "tag"
        verbose_name_plural = "tags"

    class Admin:
        fields = (
            (None, {
                'fields': ('name', 'is_visible'),
            }),
        )
        list_display = ('name', 'is_visible')
        list_filter = ('is_visible',)

    def __unicode__(self):
        return u'%s' % self.name

class Artist(models.Model):
    slug = models.SlugField(max_length=50, unique=True, prepopulate_from=('name',),)
    name = models.CharField(max_length=128)
    email = models.CharField(max_length=128, blank=True)
    #show_email = models.BooleanField(default=True, help_text="Reveal the artist's email address on the web site?")

    class Admin:
        fields = (
            (None, {
                'fields': ('name', 'slug', 'email'),
            }),
        )
        list_display = ('name', 'email')

    def __unicode__(self):
        return u'%s' % self.name
